package com.jwm.power.controller;

import cn.zhxu.bs.MapSearcher;
import cn.zhxu.bs.SearchResult;
import cn.zhxu.bs.util.MapUtils;
import com.jwm.power.model.entity.Workorders;
import com.jwm.power.model.entity.Workordertypes;
import com.jwm.power.model.reponse.ResponseData;
import com.jwm.power.service.WorksService;
import com.jwm.power.util.Ut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

//  工单管理
@Controller
@RequestMapping("/works")
public class WorkOrderController {
    @Autowired
    private MapSearcher mapSearcher;              // 注入 BeanSearcher 的检索器
    @Autowired
    private WorksService worksService;

    //  获取工单类型数据
    @RequestMapping(value = "/queryOrders", method = RequestMethod.GET)
    public void QueryOrders(HttpServletRequest request, HttpServletResponse response) {
        try {
            // 一行代码，实现一个用户检索接口（MapUtils.flat 只是收集前端的请求参数）
            String page = request.getParameter("cPage");
            String size = request.getParameter("pSize");
            String searchName = request.getParameter("searchName");
            Map<String, Object> params = MapUtils.builder()
                    .page(Integer.parseInt(page) - 1 , Integer.parseInt(size))                   // 偏移 0 条，查询 15 条（推荐写法）
                    .field("wname", searchName).op("ct")
                    .build();
            Map<String, Object> dataMap = new HashMap<>();
            SearchResult<Map<String, Object>> searchResult = mapSearcher.search(Workorders.class, params);
            dataMap.put("data", searchResult.getDataList());
            dataMap.put("totals", searchResult.getTotalCount());
            Ut.toJson(response, dataMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //  获取工单类型数据
    @RequestMapping(value = "/queryOrderTypes", method = RequestMethod.GET)
    public void QueryOrderTypes(HttpServletRequest request, HttpServletResponse response) {
        try {
            // 一行代码，实现一个用户检索接口（MapUtils.flat 只是收集前端的请求参数）
            String page = request.getParameter("cPage");
            String size = request.getParameter("pSize");
            String searchName = request.getParameter("searchName");
            Map<String, Object> params = MapUtils.builder()
                    .page(Integer.parseInt(page) - 1 , Integer.parseInt(size))                   // 偏移 0 条，查询 15 条（推荐写法）
                    .field("tname", searchName).op("ct")
                    .build();
            Map<String, Object> dataMap = new HashMap<>();
            SearchResult<Map<String, Object>> searchResult = mapSearcher.search(Workordertypes.class, params);
            dataMap.put("data", searchResult.getDataList());
            dataMap.put("totals", searchResult.getTotalCount());
            Ut.toJson(response, dataMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //  设置工单类型
    @RequestMapping(value = "saveOrderTypes", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData SaveOrderTypes(HttpServletRequest request, HttpServletResponse response) throws ParseException {
        return worksService.SaveOrderTypes(request);
    }
}
